#include "UserService.h"

bool UserService::isValidEmail(const string &str)
{
    // 判断是否为邮箱
    const string EmailPattern = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*"
                                "@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";

    regex pattern(EmailPattern); // 创建regex对象pattern进行正则表达式判断
    if (regex_match(str, pattern))
    {
        return true;
    }
    return false;
}
bool UserService::isValidPhone(const string &str)
{
    // 判断是否为手机号
    const string PhonePattern = "^1[0-9]{10}$";

    regex pattern(PhonePattern);
    if (regex_match(str, pattern))
    {
        return true;
    }
    return false;
}

int UserService::userLogin(QString loginInfo, QString password)
{

    string logininfo = loginInfo.toStdString(); // 将QString转为string

    // 邮箱判断
    if (isValidEmail(logininfo))
    {
        return UserDataAccess::userLoginEmail(loginInfo, password);
    }
    // 手机号判断
    else if (isValidPhone(logininfo))
    {
        return UserDataAccess::userLoginPhone(loginInfo, password);
    }
    return -1;
}

int UserService::userRegister(QString registerInfo, QString password)
{

    string registerinfo = registerInfo.toStdString(); // 将QString转为string

    // 邮箱判断
    if (isValidEmail(registerinfo))
    {
        return UserDataAccess::userRegisterEmail(registerInfo, password);
    }
    // 手机号判断
    else if (isValidPhone(registerinfo))
    {
        return UserDataAccess::userRegisterPhone(registerInfo, password);
    }
    return -1;
}

// 用户上传头像
bool UserService::addUserImage(int userId, const QByteArray &avatar)
{
    return UserDataAccess::addUserImage(userId, avatar);
}

// 获取用户头像
QByteArray UserService::getUserImage(int userId)
{
    return UserDataAccess::getUserImage(userId);
}

// 修改密码
bool UserService::changeUserPassword(const QString &userId, const QString &newPassword)
{
    return UserDataAccess::changePassword(userId, newPassword);
}

// 注销用户（删除用户）
bool UserService::deleteAccount(QString userId)
{
    return UserDataAccess::deleteAccount(userId);
}

// 个人信息
UserInfo UserService::getUserInfo(const int userID)
{
    return UserDataAccess::getUserInfo(userID);
}

// 个人信息修改
bool UserService::updateUserInfo(const int &userId, const QString &nickname, const QString &sex, const QString &email, const QString &sign)
{
    return UserDataAccess::updateUserInfo(userId, email, nickname, sex, sign);
}
